import axios from "axios";
import { getToken, removeToken } from "./auth";
import { Toast } from "antd-mobile";
const API = axios.create({
  baseURL: 'http://localhost:8080'
})
//添加请求拦截器
API.interceptors.request.use(config => {
  const { url } = config;
  //需要请求头才添加请求头
  if (url.startsWith('/user') &&
    !url.startsWith('/user/login') &&
    !url.startsWith('/user/registered')
  ) {
    config.headers.Authorization = getToken()
  }
  return config
})
//添加响应拦截器
API.interceptors.response.use(res => {
  console.log(res)
  if (res.data.status === 400) {
    removeToken()
  }
  if (res.config.method === 'get' && !res.config.url.includes('/user') ) {
    return res.data.body
  } else {
    return res.data
  }


}, err => {
  console.log(err);
  return err
})

export default API